package confidence.tools;

import com.github.stuxuhai.jpinyin.ChineseHelper;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringConvertUtil {

    /**
     * 全角转半角
     * 繁体转简体
     * 阿拉伯转汉字
     * @return
     */
    public static String formatText(String text) {
        try {
            return StringConvertUtil.findDigit(StringConvertUtil.convertToSimplifiedChinese(StringConvertUtil.SBC2DBC(text)));
        } catch (Exception e) {
            try {
                return StringConvertUtil.convertToSimplifiedChinese(StringConvertUtil.SBC2DBC(text));
            } catch (Exception ex) {
                return text;
            }
        }
    }

    private static String findDigit(String input) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(input);
        StringBuffer result = new StringBuffer();

        while (matcher.find()) {
            String arabicNumber = matcher.group();
            String chineseNumber = convertNumToCN(arabicNumber);
            matcher.appendReplacement(result, chineseNumber);
        }
        matcher.appendTail(result);
        return result.toString();
    }

    private static String convertNumToCN(String numStr){

        String [] nums ={"一","二","三","四","五","六","七","八","九"};
        //由于int 类型的占4个字节，所以正整数的范围为 21 4748 3647 ，所以采用 long类型 ，可以转到 千 亿
        String []unit = {" ","十","百","千","万","十","百","千","亿","十","百","千"};

        //将该字符串numStr 存到字符数组中
        char[] chars = numStr.toCharArray();
        //获取该字符数组的长度
        int length = chars.length;
        //定义接获凭借的字符串
        String str = "";
        for (int i = 0; i < length; i++) {
            char cha = chars[i];
            // 将字符 转为 int 类型
            int c = cha - '0';
            //如果数字是就不用处理（不用凭借中文数字字符和中文数字单位）
            //c 不是 0，中文数字： nums[c - 1]
            //         中文单位：unit[length - c - 1]
            if (c != 0){
                str += nums[c - 1] + unit[length - i - 1].trim();
            }
        }
        return str;
    }


    /**
     * 全角转半角
     * @param str
     * @return
     */
    private static String SBC2DBC(String str) {
        char[] c = str.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == '\u3000') {
                c[i] = ' ';
            } else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {
                c[i] = (char) (c[i] - 65248);
            }
        }
        return new String(c);
    }

    /**
     * 繁体转简体
     * @param str
     */
    private static String convertToSimplifiedChinese(String str) {
        String resStr;
        try {
            ArrayList<Integer> index = new ArrayList<>();
            String str_ = str;
            while (str_.contains("著")) {
                int idx = str_.indexOf("著");
                index.add(idx);
                if (idx == str_.length()) {
                    break;
                }
                str_ = str_.substring(idx + 1);
            }
            resStr = ChineseHelper.convertToSimplifiedChinese(str);

            if (index.size() > 0) {
                char[] strList = resStr.toCharArray();
                int idx = 0;
                for (int i : index) {
                    idx += i;
                    strList[idx] = '著';
                    idx += 1;
                }
                resStr = String.valueOf(strList);
            }
        } catch (Exception e) {
            resStr = str;
            e.printStackTrace();
        }
        return resStr;
    }



    public static void main(String[] args) {
        String text1 = "电影《２０１２》讲述了２０１２年１２月２１日的世界末日，主人公Ｊａｃｋ以及世界各国人民挣扎求生的经历，灾难面前，尽现人间百态。";
//        System.out.println(StringArabicToChinese.SBC2DBC(str));

//        String traditional = "繁體字轉換成簡體字的例子";
//        System.out.println(StringArabicToChinese.convertToSimplifiedChinese(traditional));
//        StringArabicToChinese.convertNumToCN(Long.valueOf(1111));
        String text2 = "科创21街8号";
        String text3 = "今天是3月26日，温度是20度";
        System.out.println(StringConvertUtil.formatText(text3));
    }
}


